package com.niit.az;

import android.content.Intent;
import android.database.SQLException;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;

public class Register extends AppCompatActivity {

    ListView listView;
    EditText et_name;
    EditText et_password;
    Button btn_register;
    StudentOP studentOP;
    ArrayAdapter<Object> adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        listView = (ListView)findViewById(R.id.lv_listview);
        et_name = (EditText)findViewById(R.id.et_name);
        et_password = (EditText)findViewById(R.id.et_password);
        btn_register = (Button)findViewById(R.id.btn_register);

        studentOP = new StudentOP(this);
        try{
            studentOP.open();
        }catch (SQLException e){
            e.printStackTrace();
        }
        ArrayList<Object> values = studentOP.getStuInfo();
        adapter = new ArrayAdapter<Object>(this,android.R.layout.simple_list_item_1,values);
        //listView.setAdapter(adapter);

        btn_register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String stuName = et_name.getText().toString();
                String stuPassword = et_password.getText().toString();
                if(stuName.equals("") || stuPassword.equals("")){
                    Toast.makeText(Register.this,"用户名或密码不能为空！",Toast.LENGTH_LONG).show();
                    return;
                }
                StudentBean stud = studentOP.addStudent(stuName,stuPassword);
                adapter.add(stud);
                et_name.setText("");
                et_password.setText("");
                Toast.makeText(Register.this,"恭喜您，注册成功,为您跳转至注册页面！",Toast.LENGTH_LONG).show();
                Intent myIntent=new Intent(Register.this,MainActivity.class);
                startActivity(myIntent);
            }
        });

    }
}
